
var iBytesLoaded = 0; //已上传的字节数
var iBytesTotal = 0; //总字节
var iPreviousBytesLoaded = 0; //加载的字节
var iMaxFilesize = 100048576; // 1MB  //文件的最大尺寸
var oTimer = 0;
var sResultFileSize = ''; //文档结果大小
var auth = getAuth();
var _seed = null;
var _authToken = null;
if (auth != null) {
	var _seed = getAuth().seed;
	var _authToken = getAuth().authToken;
}

var getMainTypeList = function() {
	$.ajax({
		type: "GET",
		url: "http://192.168.0.12:8080/elink_video/type/getMainType/1",
		contentType: "application/json;charset=utf-8",
		success: function(result) {
			if (result.code = 1000) {
				var mainTypeElement = document.getElementById("mainType");
				var items = result.data.list;
				var optionElement = null;
				for (var i = 0; i < items.length; i++) {
					optionElement = document.createElement("option");
					optionElement.innerText = items[i].mainTypeName;
					optionElement.setAttribute("value", items[i].mainTypeId);
					mainTypeElement.appendChild(optionElement);
				}
				mainTypeElement.addEventListener('change', function() {
					$.ajax({
						type: "GET",
						url: "http://192.168.0.12:8080/elink_video/type/getChildType/" + this.value,
						contentType: "application/json;charset=utf-8",
						success: function(result) {
							if (result.code = 1000) {
								var childTypeElement = document.getElementById("childType");
								childTypeElement.innerHtml = "";
								var childTypeIdElement = document.getElementById("childTypeId");
								var items = result.data.list;
								var optionElement = null;
								for (var i = 0; i < items.length; i++) {
									optionElement = document.createElement("option");
									optionElement.innerHTML = items[i].childTypeName;
									optionElement.setAttribute("value", items[i].childTypeId);
									childTypeElement.appendChild(optionElement);
								}
								childTypeElement.addEventListener('change', function() {
									childTypeIdElement.value = this.value;
								});
							}
						}
					});
				});
			}

		}
	});
}
getMainTypeList();

function uploadFile() {
	var pic = $("#pic").get(0).files[0];
	var formData = new FormData();
	formData.append("file", pic);
	/**
	 * auth : {
	 * 	authToken:XXX,
	 * 	seed:seed
	 * }
	 */
	//将seed和authToken传入

	formData.append("seed", _seed);
	formData.append("authToken", _authToken);

	/** 
	 * 必须false才会避开jQuery对 formdata 的默认处理 
	 * XMLHttpRequest会对 formdata 进行正确的处理 
	 */
	
				
	$.ajax({
		type: "POST",
		url: "http://192.168.0.12:8080/elink_video/video/uploadMedia",
		data: formData,
		processData: false,
		//必须false才会自动加上正确的Content-Type 
		contentType: false,
		//		   data:
		//		        JSON.stringify({
		//			        videoStorageId:globalVariable.videoStorageId
		//		    }),	
		xhr: function() {
		
			var xhr = $.ajaxSettings.xhr();
			if (onprogress && xhr.upload) {
			
				xhr.upload.addEventListener("progress", onprogress, false);
				
				return xhr;
			}
			
		},
		complete: function(result) {
			//oTimer = setInterval(doInnerUpdates, 300);

		},
		success: function(result) {

			//alert(JSON.stringify(result));
			if (result.code == 2050) {
				videoStorageId = result.data.videoStorageId;		
							
			} else {
			
			}


		}
	});
}

//获取videoStorageId;
//alert("videoStorageId="+globalVariable.videoStorageId);

//存储seed和authToken
//setAuth(seed,authToken)

//		alert('seed='+seed);
//		alert('authToken='+authToken);

//		alert("设置auth"+JSON.parse(getCookie("auth"))[authToken])
//		alert("设置seed"+JSON.parse(getCookie("auth"))[seed])

//		
//		//存储Cookie中的videoTrack对象 解释：Track跟踪 
//		var videoTrack = {
//		}
//		
//		//先定义为空
//		var myPlayer = null;
//		videojs("example_video_1").ready(function(){
//			//在初始化视频组件后赋值
//			myPlayer = this;
//			//获取Cookie中的视频进度 并在视频组件初始化后跳转进度
//			var trackTime = JSON.parse(getCookie("videoTrack"))[videoSourceId];
//			console.log(trackTime);
//			//跳转进度
//			myPlayer.currentTime(trackTime);
//		});
//
//		 
//		 
//		 
//	

/**
 * 侦查附件上传情况 ,这个方法大概0.05-0.1秒执行一次
 */
//	 function onprogress(evt){
//	  var loaded = evt.loaded;     //已经上传大小情况 
//	  var tot = evt.total;      //附件总大小 
//	  var per = Math.floor(100*loaded/tot);  //已经上传的百分比 
//	  $("#son").html( per +"%" );
//	  $("#son").css("width" , per +"%");
//	 }	 
function secondsToTime(secs) { // 将正常的时间转换为时、分、秒
	var hr = Math.floor(secs / 3600); //时
	var min = Math.floor((secs - (hr * 3600)) / 60); //分
	var sec = Math.floor(secs - (hr * 3600) - (min * 60)); //秒

	if (hr < 10) {
		hr = "0" + hr;
	}
	if (min < 10) {
		min = "0" + min;
	}
	if (sec < 10) {
		sec = "0" + sec;
	}
	if (hr) {
		hr = "00";
	}
	return hr + ':' + min + ':' + sec;
};

function bytesToSize(bytes) { //判断传入字节的大小 
	var sizes = ['Bytes', 'KB', 'MB'];
	if (bytes == 0) return 'n/a';
	var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
	return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
	//Math.pow(1024, i) 返回1024的i次幂(类似于多少个1024相乘几次,i几就相乘几)
	//.toFixed(1) 把数字转换为字符串，结果的小数点后有指定位数的数字  5.567676766===5.6
};

function onprogress(e) {
	$("#form-input").val($(".video-info-hd h4").html());
	var arr = $("#pic").val().split('\\');
	var my = arr[arr.length - 1];
	loaded = e.loaded; //已经上传大小情况 
	//alert("已上传为改变大小"+loaded);
	total = e.total; //附件总大小
	//alert("附件总大小"+total);
	var iBytesLoaded = bytesToSize(loaded); //转化后的已上传大小
	//alert("转化后的已上传"+iBytesLoaded);
	var iBytesTotal = bytesToSize(total); //转化后的总大小
	//alert("转换后的总大小"+iBytesTotal);
	var iBytesPer = Math.round(e.loaded * 100 / e.total); //转化后的上传百分比 
	$(".progress-box span").html(iBytesPer + "%");
	$(".progress").css("width", iBytesPer + "%");
	$(".uploaded em").html(iBytesLoaded);
	$(".uploaded i").html(iBytesTotal);

	//setInterval(doInnerUpdates, 300);

	var iCB = loaded; //已上传的字节
	var iDiff = iCB - iPreviousBytesLoaded; //iPreviousBytesLoaded加载的字节	
	//alert(iPreviousBytesLoaded+"加载的字节");
	// alert(iCB+"已上传的字节");
	// 如果没有新的加载-退出
	// alert(iDiff+"这是");
	if (iDiff == 0) {
		return;
	}

	iPreviousBytesLoaded = iCB;
	iDiff = iDiff * 2;
	var iBytesRem = total - iPreviousBytesLoaded; //iBytesTotal 总字节
	var secondsRemaining = iBytesRem / iDiff;

	//alert("secondsRemaining"+secondsRemaining);
	// 信息更新速度

	var iSpeed = iDiff.toString() + 'B/s';
	if (iDiff > 1024 * 1024) {
		iSpeed = (Math.round(iDiff * 100 / (1024 * 1024)) / 100).toString() + 'MB/s';
	} else if (iDiff > 1024) {
		iSpeed = (Math.round(iDiff * 100 / 1024) / 100).toString() + 'KB/s';
	}
	$(".upload-speed em").html(iSpeed); //上传速度
	$(".residual em").html(secondsToTime(secondsRemaining)); //剩余上传时间
	$(".video-info-hd h4").html(my);

}